home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2885 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.1 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: destructots and VC++
  5. Date: Fri, 19 Jan 1996 22:11:30 GMT
  6. Organization: Netcom
  7. Message-ID: <31001304.78037184@nntp.ix.netcom.com>
  8. References: <4doem9$7a8@zephyr.ens.tek.com> <30FFEDF3.2BC9@bangate.compaq.com>
  9. NNTP-Posting-Host: ix-dc6-09.ix.netcom.com
  10. X-NETCOM-Date: Fri Jan 19  2:11:29 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. Saurabh Dixit <saurabhd@bangate.compaq.com> wrote:
  14.  
  15. > Err...
  16. > I wasn't even aware that constructors/destructors could 
  17. > be invoked directly on objects from outside.
  18. > In fact I remember a few years back when I was in school
  19. > I tried to call the constructor on one of my objects to
  20. > re-initialize it and when my gnu++ croaked someone responded
  21. > to my query in this newsgroup to set me straight.
  22. > Regarding VC++ I know the following works in 4.0
  23. > class A {
  24. >     int a;
  25. >     int b;
  26. > public:
  27. >     A (int _a) 
  28. >     {
  29. >         a = 4;
  30. >         A ();
  31. >     }
  32. >     A () 
  33. >     {
  34. >         b = 4;
  35. >     };
  36. > };
  37. > So from within the class, calling a constructor seems to work.
  38. > So maybe I'm thinking constructors and destructors can be
  39. > explicitly invoked from within the class but not from outside?
  40. > Anyone care to comment?
  41.  
  42. This will work on any conforming C++ compiler in that it is legal code
  43. and the semantics are well defined.  However, it is very unlikely that
  44. it is doing what you want.
  45.  
  46. A() never calls the  constructor for an existing object.  It creates a
  47. temporary, constructs it using the default constructor, and destroys
  48. it.  In your example, if you write
  49.  
  50.     A x(1);
  51.  
  52. will construct x with x.a initialized to 4 and x.b uninitialized (or
  53. initialized to 0 if it is statically allocated).  It will also
  54. construct a temporary A object with x.a uninitialized and x.b
  55. initialized to 4 and will destroy that object.
  56.  
  57. > Of course, I may be full of .... since the original poster
  58. > claims calling a destructor explicitly from outside is 
  59. > supposed to work as it is the standard.
  60.  
  61. The rules for a destructor are different.  It is possible to call the
  62. destructor explicitly for an object.
  63.  
  64.  
  65. Michael M Rubenstein
  66.